import Vue from 'vue'
import VueRouter from 'vue-router'
import Welcome from '../views/enterPage/welcome'
import Login from '../views/enterPage/login'
import Agreement from '../views/enterPage/agreement'
import Main from '../views/mainPage/main'
import Search from '../views/mainPage/indexPage/search'
import PicDesc from '../views/mainPage/indexPage/picdesc'
import indexPage from "@/views/mainPage/indexPage/indexPage";
import findPage from "@/views/mainPage/findPage/findPage";
import userInfo from "@/views/mainPage/indexPage/userInfo";
import fans from "@/views/mainPage/indexPage/fans";
import talk from "@/views/mainPage/indexPage/talk";
import workCollection from "@/views/mainPage/findPage/workCollection";
import info from "@/views/mainPage/message/info";
import like from "@/views/mainPage/message/like";
import messagePage from "@/views/mainPage/message/messagePage";
import flowing from "@/views/mainPage/indexPage/flowing";
import history from "@/views/mainPage/minePage/history";
import xhProcess from "@/views/mainPage/minePage/xhProcess";
import help from "@/views/mainPage/minePage/help";
import setting from "@/views/mainPage/minePage/setting";
import minePage from "@/views/mainPage/minePage/minePage";
import workShow from "@/views/mainPage/indexPage/workShow";
import editInfo from "@/views/mainPage/minePage/editInfo";
import Drawing from "@/views/mainPage/minePage/Drawing";
import upload from "@/views/mainPage/minePage/upload";
Vue.use(VueRouter)

const routes = [
  {
    path: '/welcome',
    name: 'welcome',
    component: Welcome,
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/agreement',
    name: 'agreement',
    component: Agreement
  },
  {
    path: '/',
    name: 'main',
    component: Main,
    redirect: '/indexPage',
    children:[
      {
        path:"indexPage",
        name:"indexPage",
        component:indexPage
      },
      {
        path:"findPage",
        name:"findPage",
        component:findPage
      },
      {
        path:"messagePage",
        name:"messagePage",
        component:messagePage
      },{
        path:"minePage",
        name:"minePage",
        component:minePage
      }
    ]
  },
  {
    path: '/search',
    name: 'search',
    component: Search,
  },
  {
    path: '/picdesc/:id',
    name: 'picdesc',
    component: PicDesc
  },{
    path: '/userInfo/:id',
    name: 'userInfo',
    component: userInfo
  },
  {
    path: '/fans/:id',
    name: 'fans',
    component: fans
  },
  {
    path: '/talk/:id',
    name: 'talk',
    component: talk
  },
  {
    path: '/workCollection/:id',
    name: 'workCollection',
    component: workCollection
  },
  {
    path: '/info/:id',
    name: 'info',
    component: info
  },
  {
    path: '/like/:id',
    name: 'like',
    component: like
  },{
    path: '/flowing/:id',
    name: 'flowing',
    component: flowing
  },
  {
    path: '/upload',
    name: 'upload',
    component: upload
  },
  {
    path: '/workShow/:id',
    name: 'workShow',
    component: workShow
  },
  {
    path: '/editInfo/:id',
    name: 'editInfo',
    component: editInfo
  },
  {
    path: '/Drawing/:id',
    name: 'Drawing',
    component: Drawing
  },
  {
    path: '/history/:id',
    name: 'history',
    component: history
  },
  {
    path: '/xhProcess/:id',
    name: 'xhProcess',
    component: xhProcess
  },
  {
    path: '/help',
    name: 'help',
    component: help
  },
  {
    path: '/setting',
    name: 'setting',
    component: setting
  }


]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫，判断页面的登录是否有token
router.beforeEach((to,from,next)=>{
  /**
   * to是将要访问的路径
   * from是从哪个路径跳转而来
   * next()是一个函数，表示放行
   * next() 放行   next('/login')强制跳转
   * */
  if (to.path=='/welcome')return next()
  else if (to.path=='/login')return next()
  else if (to.path=='/agreement')return next()
  //获取token
  var tokenStr=window.sessionStorage.getItem('token')
  if(!tokenStr)return next('/welcome')
  next()
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
